package org.tio.sitexxx.web.server.controller.kf;


import com.jfinal.plugin.activerecord.Record;
import org.tio.http.server.annotation.RequestPath;
import org.tio.sitexxx.service.model.main.Blog;
import org.tio.sitexxx.service.model.main.ConsultantMemberValid;
import org.tio.sitexxx.service.model.main.ConsultantServiceStaff;
import org.tio.sitexxx.service.model.main.Member;
import org.tio.sitexxx.service.service.kf.InformationService;
import org.tio.utils.json.Json;
import org.tio.utils.resp.Resp;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RequestPath(value = "/qj/kf")
public class InformatinController {



    private static final Member meb = new Member().dao();
    private static final ConsultantMemberValid consultantMemberValid = new ConsultantMemberValid().dao();

    public InformatinController(){};

    @RequestPath("/init")
    public Resp getInformation(String openId) {
        System.out.println("getInformation-----openid="+openId);
        try {
            Map<String, Object> init = new HashMap<>();
            Map<String, Object> mine = new HashMap<>();
            Map<String, Object> friend1 = new HashMap<>();
            List<Map<String, Object>> friend = new ArrayList<>();
            List<Map<String, Object>> fList = new ArrayList<>();
            friend1.put("id", 1);
            ConsultantServiceStaff cs = ConsultantServiceStaff.dao.findFirst("select * from consultant_service_staff where openid=?", openId);
            if (cs == null) {//为member
                mine.put("type","m");
                Member mMine = meb.findFirst("select * from Member where openid=?", openId);
                mine.put("id",mMine.getId());
                mine.put("avatar",mMine.getAvatar());
                mine.put("username",mMine.getName());
                List<ConsultantServiceStaff> allConsult = ConsultantServiceStaff.dao.find("select *  from consultant_service_staff");
                for (ConsultantServiceStaff con : allConsult) {
                    Map<String, Object> map = new HashMap<>();
                    map.put("id", con.getId());
                    map.put("username", con.getName());
                    map.put("avatar", con.getAvata());
                    System.out.println(map.toString());
                    fList.add(map);
                    friend1.put("groupname", "我的顾问");
                }
            } else {//为consultant或者custom service
                mine.put("type","c");
                mine.put("id", cs.getId());
                mine.put("avatar", cs.getAvata());
                mine.put("username", cs.getName());
                List<ConsultantMemberValid> list = consultantMemberValid.find("select * from Consultant_member_valid where consultant_id=? and expired=1", mine.get("id"));
                Map<String, Object> mperson = new HashMap<>();
                for (ConsultantMemberValid conv : list) {
                    Member mf = meb.findFirst("select * from member where id=?", conv.getMemberId());
                    mperson.put("id", mf.getId());
                    mperson.put("avatar", mf.getAvatar());
                    mperson.put("username", mf.getName());
                    friend1.put("groupname", "我的顾客");
                    fList.add(mperson);
                }
            }
            friend1.put("list", fList);
            friend.add(friend1);
            init.put("mine", mine);
            init.put("friend", friend);
            return Resp.ok(init);
        }catch (Exception e){
            e.printStackTrace();
            return Resp.fail("not find");
        }
    }


    public static void main(String[] args){
        Map<String,Object> test = new HashMap<>();
        test.put("id",34534);
        System.out.println(test.get("id"));
        test.put("id",244489826);
        System.out.println(test.get("id"));
    }

}
